package com.tencreat.mall.controller.app;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tencreat.common.core.HttpResult;
import com.tencreat.common.core.controller.BaseController;
import com.tencreat.common.core.domain.model.AccountLogin;
import com.tencreat.common.core.domain.model.IdAndPage;
import com.tencreat.common.core.domain.model.ReqStringParam;
import com.tencreat.common.enums.BalanceBusinessType;
import com.tencreat.common.utils.StringUtils;
import com.tencreat.common.utils.spring.SpringUtils;
import com.tencreat.domain.mall.BalanceChange;
import com.tencreat.domain.mall.GoodsRollOrder;
import com.tencreat.domain.mall.RechargePackage;
import com.tencreat.domain.mall.vo.*;
import com.tencreat.domain.mall.dto.BalanceRecordDTO;
import com.tencreat.domain.mall.dto.RechargeOrderDTO;
import com.tencreat.service.mall.IBalanceChangeService;
import com.tencreat.service.mall.IMallOrderService;
import com.tencreat.service.mall.IRechargeOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * r充值(提现)订单Controller
 *
 * @author wangkai
 * @date 2021-11-13
 */
@Api(tags = "92.充值、消费券api")
@RestController
@RequestMapping("/charge/api")
public class RechargeOrderController extends BaseController {
    @Autowired
    IRechargeOrderService rechargeOrderService;
    @Autowired
    private IBalanceChangeService balanceChangeService;

    @Autowired
    private IMallOrderService mallOrderService;

    @PostMapping("/recharge")
    @ApiOperation("app端充值")
    public HttpResult<?> recharge(@ApiParam("充值参数对象") @Validated @RequestBody RechargeOrderDTO vo) throws Exception{
        Long uuid = getUuid();
        return HttpResult.ok(rechargeOrderService.recharge(vo, uuid,getLoginCheck()));
    }


    @GetMapping("/getMyPurseInfo")
    @ApiOperation("app我的钱包")
    public HttpResult<ResMyPurseInfoVO> getMyPurseInfo(@ApiParam("uuid") @RequestParam Long uuid) {
        return HttpResult.ok(rechargeOrderService.getMyPurseInfo(uuid));
    }


    @ApiOperation("充值套餐显示")
   // @PostMapping("/getRechargePackageList")
    public HttpResult<List<RechargePackage>> getRechargePackageList(){
        return HttpResult.ok(rechargeOrderService.getRechargePackageList());
    }


    @ApiOperation("充值回显显示")
    //@PostMapping("/getRechargePackage")
    public HttpResult<RechargePackage> getRechargePackage(BigDecimal money){
        return HttpResult.ok(rechargeOrderService.selectrRechargePackage(money));
    }

    /**
     * 余额支付密码验证
     */
    @ApiOperation("app端余额支付密码验证")
    @PostMapping("/balance/validation")
    public HttpResult<?> balanceValidation(@RequestBody ReqStringParam id) {
        return rechargeOrderService.balanceValidation(id, getLoginCheck());
    }

    /**
     * 查询余额变动列表
     */
    @ApiOperation(value="查询消费券/货款/余额变动列表")
    @PostMapping("/balance/list")
    public HttpResult<IPage<BalanceChange>> list(@RequestBody  BalanceChange balanceChange) {
        balanceChange.setUuid(getLoginCheck().getAccountLogin().getUuid());
//        balanceChange.setUuid(652649L);
        QueryWrapper<BalanceChange> wrapper = Wrappers.query(balanceChange);
        if (StringUtils.isNotEmpty(balanceChange.getSearchDate())){
            wrapper.apply("date_format(create_time,'%Y-%m') = "+ "'"+balanceChange.getSearchDate()+"'");
        }
        Page<BalanceChange> page =  new Page<>(balanceChange.getPageNum(), balanceChange.getPageSize());
        return HttpResult.ok(balanceChangeService.page(page, wrapper.lambda().orderByDesc(BalanceChange::getCreateTime)));
    }


    /**
     * 描述信息  查询累计充值记录
     * @Author yaoruiqi
     * @Date: 2023/10/25 02:49
     */
    @ApiOperation(value = "累计充值记录")
    @PostMapping("/accumulatedRecharge")
    public HttpResult<AccumulatedVO>  accumulatedRecharge(){
        AccountLogin accountLogin = getLoginCheck().getAccountLogin();

        return HttpResult.ok(balanceChangeService.accumulatedRecharge(accountLogin));
    }

    /**
     * 描述信息  查询累计消费记录
     * @Author yaoruiqi
     * @Date: 2023/10/25 02:49
     */
    @ApiOperation(value = "累计消费记录")
    @PostMapping("/accumulatedConsumer")
    public HttpResult<AccumulatedVO>  accumulatedConsumer(){
        AccountLogin accountLogin = getLoginCheck().getAccountLogin();

        return HttpResult.ok(balanceChangeService.accumulatedConsumer(accountLogin));
    }

    @ApiOperation(value = "充值记录")
    @PostMapping("/rechargeRecord")
   public HttpResult<IPage<ResOrderGoodsVO>>  rechargeRecord(@RequestBody IdAndPage idParam){
        return HttpResult.ok(mallOrderService.rechargeRecord(idParam,getLoginCheck().getAccountLogin().getUuid()));
   }

    @ApiOperation(value="查询提货单变动列表")
    @PostMapping("/balance/goodsRollList")
    public HttpResult<IPage<GoodsRollOrder>> goodsRollList(@RequestBody  GoodsRollOrder goodsRollOrder) {
        GoodsRollOrder goodsRollOrder1 = goodsRollOrder.setUuid(getLoginCheck().getAccountLogin().getUuid());
//        GoodsRollOrder goodsRollOrder1 = goodsRollOrder.setUuid(652649L);

        Page<GoodsRollOrder> page =  new Page<>(goodsRollOrder.getPageNum(), goodsRollOrder.getPageSize());
        return HttpResult.ok(balanceChangeService.goodsRollList(page, goodsRollOrder1));
    }



    @ApiOperation(value = "订单管理数据统计")
    //@GetMapping("/orderStatistics")
    public HttpResult<MallAdminOrderStatisticsVO> orderStatistics(@ApiParam("订单标记 4-虚拟订单,1-自提") @RequestParam(required = false) Integer orderMold){
        return HttpResult.ok( mallOrderService.orderStatistics(null,orderMold));
    }


}
